﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogRecentActivity
    {
        #region Properties
        public User User
        {
            get
            {
                return this.GetUser();
            }
        }
        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods
        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static List<LogRecentActivity> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogRecentActivities.OrderByDescending(p=>p.ActionTime).ToList();
        }

        public static List<ErrorItem> Insert(int id_User, int id_Action, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.AddRecentActivity(id_User, id_Action);
            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion
    }
}